package com.youlai.boot.bmp.controller;

import com.aizuda.bpm.engine.FlowLongEngine;
import com.aizuda.bpm.engine.core.FlowCreator;
import com.aizuda.bpm.engine.entity.FlwHisInstance;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.boot.bmp.model.form.BizProcessForm;
import com.youlai.boot.bmp.model.form.InstanceForm;
import com.youlai.boot.bmp.model.query.BizInstanceQuery;
import com.youlai.boot.bmp.model.query.BizProcessCategoryQuery;
import com.youlai.boot.bmp.model.vo.BizProcessCategoryListVo;
import com.youlai.boot.bmp.service.BizProcessService;
import com.youlai.boot.core.web.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Tag(name = "20.工作流程定义接口")
@RestController
@RequestMapping("/api/v1/process")
@AllArgsConstructor
public class BizProcessController {


    private final BizProcessService bizProcessService;

    private final FlowLongEngine flowLongEngine;


    /**
     * @deprecated 测试使用
     *
     * @return 列表
     */
    @Deprecated
    @Operation(summary = "工作流列表")
    @PostMapping("save")
    public Result<?> createProcessList() {
        flowLongEngine.processService().deployByResource("process.json", FlowCreator.ADMIN, false);
        return Result.success(bizProcessService.getProcessList());
    }


    @Operation(summary = "模型修改保存")
    @PostMapping("/model/save")
    public Result<Void> createProcessMode(@Validated @RequestBody BizProcessForm form) {
        //todo 此处校验前端是否提交对应负责人，如未提交获取当前登录用户
        if (StringUtils.isBlank(form.getCreateId())){
            form.setCreateId("12398795");
        }
       if (StringUtils.isBlank(form.getCreateBy())){
           form.setCreateBy("admin");
       }
        Long processId = bizProcessService.deployBizProcess(form);
        return Result.judge(processId > 0);
    }

    @Operation(summary = "根据分类获取流程列表(分页)")
    @GetMapping("/category/list")
    public Result< Page<BizProcessCategoryListVo>> getCategoryProcessPageList(BizProcessCategoryQuery queryParams) {
        //todo 此处需要根据权限获取不同的分类列表
        Page<BizProcessCategoryListVo> res =  bizProcessService.getBizProcessCategoryList(queryParams);
        return Result.success(res);
    }


    @Operation(summary = "切换流程状态")
    @PostMapping("/switch")
    public Result<Void> switchProcessStatus(@Parameter(description = "流程processId") @RequestParam Long processId, @Parameter(description = "流程状态0启用，1停用") @RequestParam Integer status) {
        if (status != 1 && status != 0) {
            return Result.failed("参数错误");
        }
        boolean res = bizProcessService.switchProcessStatus(processId, status);
        return Result.judge(res);
    }

    @Operation(summary = "启动流程实例")
    @PostMapping("/start")
    public Result<Void> startProcessInstance(@RequestBody InstanceForm  instanceForm) {
        boolean b = bizProcessService.startInstance(instanceForm, 410000199512025445L);
        if (!b) {
            return Result.failed("启动流程失败");
        }
        return Result.judge(true);
    }


    /**
     * 获取当前用户流程实例列表
     * @param instanceQuery 查询参数
     * @return 分页参数
     */
    @Operation(summary = "获取当前用户发起申请列表")
    @PostMapping("/own/instance")
    public Result<Page<FlwHisInstance>> getInstancePageList(@RequestBody BizInstanceQuery instanceQuery){
        Page<FlwHisInstance> instancePage = bizProcessService.getInstanceList(410000199512025445L, instanceQuery);
        return Result.success(instancePage);
    }


}
